package com.anosine.gpsl.android.util;

import java.util.Locale;

//import com.anosine.gpsl.android.AppConfig;
import com.anosine.gpsl.android.GPSLApp;

import android.content.Context;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * @Title: NetStateManager
 * @Package com.anosine.gpsl.android.util
 * @Description: 网络管理
 * @author: sunny
 * @date: 2015-11-19
 * @version V1.0
 */
public class NetStateManager {
	public enum NetState {
        MOBILE, WIFY, NOWAY
    }

    public static NetState CUR_NETSTATE = NetState.NOWAY;

    public static String MOBILE_PROXY = null;

    private static NetStateReceiver sReceiver;

    public static void init(Context context) {
        CUR_NETSTATE = NetState.NOWAY;
        getCurNetState(context);
        sReceiver = null;
        IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        sReceiver = new NetStateReceiver();
        context.registerReceiver(sReceiver, filter);
    }

    public static void onExit(Context context) {
        if (sReceiver != null) {
            context.unregisterReceiver(sReceiver);
            CUR_NETSTATE = NetState.NOWAY;
            sReceiver = null;
        }
    }

    public static boolean OnNet() {
        getCurNetState(GPSLApp.getApplication());
        return CUR_NETSTATE != NetState.NOWAY;
    }

    protected static NetState getCurNetState(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null) {
            int netType = networkInfo.getType();
            int subType = networkInfo.getSubtype();
            if (isConnectionFast(netType, subType)) {
                NetStateManager.CUR_NETSTATE = NetState.WIFY;
            } else {
                NetStateManager.CUR_NETSTATE = NetState.MOBILE;
            }
        } else {
            NetStateManager.CUR_NETSTATE = NetState.NOWAY;
        }
        return NetStateManager.CUR_NETSTATE;
    }

    protected static boolean isConnectionFast(int type, int subType) {
        if (type == ConnectivityManager.TYPE_WIFI) {
            return true;
        } else {
            return false;
        }
    }
	/**
	 * 查看是否有网络链接
	 * 
	 * @return
	 */
	public boolean isNetworkConnected(Context context) {
		ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cm.getActiveNetworkInfo();
		return ni != null && ni.isConnectedOrConnecting();
	}
	/**
	 * 获取当前网络类型
	 * 
	 * @return 0：没有网络 1：WIFI网络 2：WAP网络 3：NET网络
	 */
//	public static int getNetworkType(Context context) {
//		int netType = 0;
//		ConnectivityManager connectivityManager = (ConnectivityManager) context
//				.getSystemService(Context.CONNECTIVITY_SERVICE);
//		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
//		if (networkInfo == null) {
//			return netType;
//		}
//		int nType = networkInfo.getType();
//		if (nType == ConnectivityManager.TYPE_MOBILE) {
//			String extraInfo = networkInfo.getExtraInfo();
//			if (!StringUtil.isEmpty(extraInfo)) {
//				if (extraInfo.toLowerCase(Locale.US).equals("cmnet")) {
//					netType = AppConfig.NETTYPE_CMNET;
//				} else {
//					netType = AppConfig.NETTYPE_CMWAP;
//				}
//			}
//		} else if (nType == ConnectivityManager.TYPE_WIFI) {
//			netType = AppConfig.NETTYPE_WIFI;
//		}
//		return netType;
//	}
}
